/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!				\file		CC_GraphItemProp.cc
 \date		june 2010
 \author		TNick

 \brief		Implements CC_GIP class


	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Please read ReadMe.txt and Licence.txt in root folder
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"../main.h"

#include	<assert.h>

#include	"CC_App.h"
#include	"CC_GraphItemProp.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */


/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */




/* ------------------------------------------------------------------------- */
/* bind an item to a generic GIP                                             */
CCERR::EERRORS		CC_GIP::GetGenericItem
(CC_Draw* pD, CC_GIP** pOut, CC_Err* pErr)
	{

	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;
	CC_GIP*	pRet;

	if ((pD == NULL) || (!pD->InitOk()) || (pOut == NULL))
		{
		Q_ASSERT(0);
		pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
				  TR::tr("Generic item: no drawing provided"));
		return CCERR::ERR_STATUS;
		}

	/* check for default item; we may create it here if it does not */
	if (pD->GphTyp.count() == 0)
		{
		pRet = new CC_GIP(pD);
		pRet->p.col.setRgb(0, 0, 0);
		pRet->p.lnTy = 0;
		pRet->p.lnW = 0;
		pRet->p.flg = static_cast<GIP::IFLG>
				(GIP::COLORED | GIP::LNTYPED | GIP::WEIGHTED);
		}

	*pOut = static_cast<CC_GIP*>( pD->GphTyp.item(0));
	return bRet;
	}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
/* bind an item to a generic GIP                                             */
CCERR::EERRORS		CC_GIP::GetSpecificItem	(CC_Draw* pD, GIP::ITEM* pRq,
											 GIP::ITMBR val_flg,
											 GIP::IFLG flg_mask,
											 CC_GIP** pOut, CC_Err* pErr)
	{

	CC_GIP*		pItr;
	GIP::ITEM	srcGip;

	if ((pD == NULL) || (!pD->InitOk()) || (pOut == NULL))
		{
		Q_ASSERT(0);
		pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
				  TR::tr("Specific item: no drawing provided"));
		return CCERR::ERR_STATUS;
		}

	/* check for default item; we may create it here if it does not */
	if (pD->GphTyp.count() == 0)
		{
		pItr = new CC_GIP(pD);
		pItr->p.col.setRgb(0, 0, 0);
		pItr->p.lnTy = 0;
		pItr->p.lnW = 0;
		pItr->p.flg = static_cast<GIP::IFLG>
				(GIP::COLORED | GIP::LNTYPED | GIP::WEIGHTED);
		}

	/* create a search structure by commbining requested elements with
   generic item */
	pItr =static_cast<CC_GIP*>(pD->GphTyp.item(0));
	srcGip = pItr->p;
	if (val_flg & GIP::COLOR)
		{
		srcGip.col = pRq->col;
		}
	if (val_flg & GIP::LINEH)
		{
		srcGip.lnTy = pRq->lnTy;
		}
	if (val_flg & GIP::LINEW)
		{
		srcGip.lnW = pRq->lnW;
		}
	if (val_flg & GIP::ITFLAGS)
		{
		srcGip.flg = static_cast<GIP::IFLG>
				(srcGip.flg | (pRq->flg & flg_mask));
		}


	/* loop in all structures checking for a match */
	for (int i = 0; i < pD->GphTyp.count(); i++ )
		{
		pItr = static_cast<CC_GIP*>( pD->GphTyp.item(i));
		if (pItr->p == srcGip)
			{
			*pOut = pItr;
			return CCERR::ERR_SUCCESS;
			}
		}


	/* no match in the search; create a new one */
	pItr = new CC_GIP(pD);
	pItr->p = srcGip;
	*pOut = pItr;
	return CCERR::ERR_SUCCESS;
	}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
